﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Gds.ViewModel.pageViewModel;
namespace Gas_Server_framework
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            
            MainViewModel.Instance.StartTaksEvent += Instance_StartTaksEvent;
            MainViewModel.Instance.StopTaksEvent += Instance_StopTaksEvent;
            MainViewModel.Instance.OpenClentConfigViewEvent += Instance_OpenClentConfigViewEvent;
            init();
            this.DataContext = MainViewModel.Instance;
        }

        private void Instance_OpenClentConfigViewEvent(object sender, EventArgs e)
        {
            PageView.ClientModelPortConfigView clientModelPortConfigView = new PageView.ClientModelPortConfigView();
            clientModelPortConfigView.ShowDialog();
        }

        Gds.TaskFunction.TestFunction.ServerTestFcuntion serverTestFcuntion = new Gds.TaskFunction.TestFunction.ServerTestFcuntion();

        /// <summary>
        /// 停止通讯线程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void Instance_StopTaksEvent(object sender, EventArgs e)
        {
            serverTestFcuntion.Dispose();
        }
        /// <summary>
        /// 开始通讯线程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Instance_StartTaksEvent(object sender, EventArgs e)
        {
          serverTestFcuntion.StartTask();
        }

        private void init()
        {
          
            MainViewModel.Instance.Init();
        }

        #region 打开界面
        private void btn_configShow_Click(object sender, RoutedEventArgs e)
        {
            PageView.ViewConfig viewConfig = new PageView.ViewConfig();
            viewConfig.ShowDialog();
        }
        #endregion

        private void btn_testFunction_Click(object sender, RoutedEventArgs e)
        {
            //if( MainViewModel.Instance.OnLineClientList==null) MainViewModel.Instance.OnLineClientList = new Gds.Model.AsyncObservableCollection<Gds.Model.ConfigModel.BasicSystemInfo.UserInfo>();
            // MainViewModel.Instance.OnLineClientList.Add(new Gds.Model.ConfigModel.BasicSystemInfo.UserInfo()
            // {
            //     UserName = "Test_" + (MainViewModel.Instance.OnLineClientList.Count + 1),
            // });
 
            MainViewModel.Instance.ReadSplit = int.Parse(ServerConfigViewModel.Instance.LocalConfig.ReadSplit);

        }
        /// <summary>
        /// 软件关闭中执行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            MainViewModel.Instance.AddLogModel("软件关闭");
            MainViewModel.Instance.ExitFlag = true;
        }

        private void btn_showCommunicationLog_Click(object sender, RoutedEventArgs e)
        {
            PageView.ViewShowLog viewCommunicateShow = new PageView.ViewShowLog();
            viewCommunicateShow.ShowDialog();
        }

        private void btn_ctrlDelhis_Click(object sender, RoutedEventArgs e)
        {
            MainViewModel.Instance.StartDelData = true;
        }
    }
}
